﻿Imports VSModel
Imports Cls_Common
Partial Class Control_Contents_AlbumDetail
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            hidId.Value = Page.RouteData.Values("Id")
            ShowDataDEtail(hidId.Value)
            BindPostedNews(1)

        End If
    End Sub

    Sub ShowDataDEtail(ByVal iobjId As Integer)
        Using data As New VSEntities
            Dim p = (From c In data.Contents Where c.Id = iobjId And c.StatusId = 3 Select c).SingleOrDefault
            If Not p Is Nothing Then


                If (Session("PageView") Is Nothing) Then
                    Session("PageView") = Now
                    'Update so luot xem len 1
                    p.Viewed = IIf(IsNothing(p.Viewed) = True, 1, p.Viewed + 1)
                    data.SaveChanges()
                Else
                    If (DateDiff(DateInterval.Second, CType(Session("PageView"), Date), Now()) >= ConfigurationManager.AppSettings("SettingTimeUpdatePageView")) Then
                        'Update so luot xem len 1
                        p.Viewed = IIf(IsNothing(p.Viewed) = True, 1, p.Viewed + 1)
                        data.SaveChanges()
                        Session("PageView") = Now
                    End If
                End If
                Dim dtDate As DateTime
                dtDate = CType(p.PublishedDate, DateTime)
                '   ltrViewAndUpdateDate.Text = arrDateOfWeekVie(dtDate.DayOfWeek()) & ", " & dtDate.ToString("dd/MM/yyyy HH:mm")

                '  ltrAuthor.Text = IIf(IsNothing(p.Author), "", p.Author)

                hplTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
                Page.Title = hplTitle.Text & " | " & DomainName
                Page.MetaDescription = IIf(IsNothing(p.Description) = True, "", p.Description)
                ltrAuthor.Text = IIf(IsNothing(p.Author) = True, "", p.Author)

                ltrDescription.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
                Dim t = (From q In data.Albums Where q.ContensID = iobjId Select q).ToList
                With rptSlideShow
                    .DataSource = t
                    .DataBind()
                End With





            End If
        End Using
    End Sub

    

    Protected Sub BindPostedNews(ByVal iPage As Integer)
        Dim _lang As Integer = Session("LanguageId")
        Using Data As New VSEntities
            'load tin da dua
            Dim h = (From q In Data.Contents Where q.LanguageId = _lang And q.StatusId = 3 _
                                                        And q.Id <> hidId.Value And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                        Or q.eEffectiveDate Is Nothing) _
                                                    Order By q.PublishedDate Descending Select q.Title, q.Id, q.QueryString, q.MenuId, q.TypeId, q.PublishedDate).Skip((iPage - 1) * 10).Take(10)
            hidCount.Value = (From q In Data.Contents Where q.LanguageId = _lang And q.StatusId = 3 _
                                                       And q.Id <> hidId.Value And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                       Or q.eEffectiveDate Is Nothing) _
                                                   Order By q.PublishedDate Descending Select q.Id).Count


            'Tong so ban ghi
            If hidCount.Value > 0 Then
                Create_Pager(hidCount.Value, iPage, 10, 10)
            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If

            With rptPortedNews
                .DataSource = h
                .DataBind()
            End With
        End Using
    End Sub
    Protected Sub rptPortedNews_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPortedNews.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim lblDate As Label = CType(e.Item.FindControl("lblDate"), Label)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & row.MenuId & "/" & row.QueryString & "-" & row.Id.ToString & arrTypeLink(row.TypeId) & ".html"
        lblDate.Text = DateToString(row.PublishedDate, "mm/dd/yyyy")



    End Sub
    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        'Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        'ScriptManager.RegisterAsyncPostBackControl(lnkTitle)

        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)

        BindPostedNews(CInt(lnkTile.ToolTip))
        lnkLast.Enabled = True
        lnkFirst.Enabled = True

        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindPostedNews(hidCur_Page.Value)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True

        If hidCur_Page.Value = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf hidCur_Page.Value = 1 Then
            lnkFirst.Enabled = False
        End If

    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindPostedNews(hidCur_Page.Value)

        lnkLast.Enabled = True
        lnkFirst.Enabled = True

        If hidCur_Page.Value = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf hidCur_Page.Value = 1 Then
            lnkFirst.Enabled = False
        End If

    End Sub

End Class
